package com.campos.collection;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;

import androidx.core.app.ActivityCompat;
import com.campos.common.*;


import java.text.SimpleDateFormat;

public class GPSWriter {

    public Context context = null;
    private Handler mHandler = null;
    int status = 0;
    public void start(){
        status = 0;
        mHandler = new Handler();
        Util.CreateDir(Config.instance().getOrCreateProjectPath());
        Runnable r = new Runnable() {
             @Override
             public void run() {
                 final Location gps = LocationUtils.getGPSLocation(context);
                 LocationUtils.addLocationListener(context, LocationManager.GPS_PROVIDER, new LocationUtils.ILocationListener() {
                     @Override
                     public void onSuccessLocation(Location location) {
                         writeGPSInfo(location);
                     }
                 });
                 if(status != 1){
                     //do something
                     //每隔1s循环执行run方法
                     mHandler.postDelayed(this, 1000);
                 }

             }
        };
        mHandler.postDelayed(r, 1000);
    }

    public void stop(){
        this.status = 1;
    }
    public void writeGPSInfo(Location gps){
        double lon = gps.getLongitude();
        double lat = gps.getLatitude();
        double hei = gps.getAltitude();
        double acc = gps.getAccuracy();
        double gpst = gps.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String gpstimestr = simpleDateFormat.format(gpst);

        String gpsinfo = String.format("%s,%f,%f,%f",gpstimestr,lon,lat,hei);
        String path = Config.instance().getOrCreateProjectPath() + "GPSlist.txt";
        Util.WriteTxtFile(gpsinfo,path);
    }
}
